package com.ruoyi.quartz.task;

import com.ruoyi.quartz.util.TaskUtils;
import com.ruoyi.task.domain.KwTaskInfo;
import com.ruoyi.task.service.IKwTaskInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 主机任务
 * 
 * @author gonianer
 */
@Component("hostTask")
public class HostTask
{
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IKwTaskInfoService taskInfoService;
    @Autowired
    private HostTaskHandler addHostTask;

    @Transactional(rollbackFor = Exception.class)
    public void addHost(Long hostId){
        if(hostId == null) {
            logger.warn("add host failed ,hostId is null");
            return;
        }
        //添加任务步骤
        KwTaskInfo t = TaskUtils.buildTask(hostId,0L,"addHost","添加主机",0);
        taskInfoService.insertKwTaskInfo(t);
        //添加子任务
        KwTaskInfo t1 = TaskUtils.buildTask(hostId,t.getId(),"checkHostCreat","创建主机预检查",0);
        taskInfoService.insertKwTaskInfo(t1);
        KwTaskInfo t2 = TaskUtils.buildTask(hostId,t.getId(),"syncHostInfo","同步主机信息",0);
        taskInfoService.insertKwTaskInfo(t2);
        KwTaskInfo t3 = TaskUtils.buildTask(hostId,t.getId(),"createJwagentCredential","创建jwagent凭证",0);
        taskInfoService.insertKwTaskInfo(t3);
        KwTaskInfo t4 = TaskUtils.buildTask(hostId,t.getId(),"uploadJwagentSource","上传jwagent资源",0);
        taskInfoService.insertKwTaskInfo(t4);
        KwTaskInfo t5 = TaskUtils.buildTask(hostId,t.getId(),"installJwagent","安装jwagent",0);
        taskInfoService.insertKwTaskInfo(t5);
        KwTaskInfo t6 = TaskUtils.buildTask(hostId,t.getId(),"checkJwagentStatus","检查jwagent状态",0);
        taskInfoService.insertKwTaskInfo(t6);
        KwTaskInfo t7 = TaskUtils.buildTask(hostId,t.getId(),"updateHostStatus","更新主机状态",0);
        taskInfoService.insertKwTaskInfo(t7);

        //开始执行任务
        addHostTask.addHost(t,false);
    }

    @Transactional(rollbackFor = Exception.class)
    public void rollbackHost(Long taskId){
        if(taskId == null) {
            logger.warn("rollback host failed ,taskId is null");
            return;
        }
        //开始执行任务
        addHostTask.rollbackHost(taskId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void retryAddHost(Long taskId){
        if(taskId == null) {
            logger.warn("retry add host failed ,taskId is null");
            return;
        }
        //开始执行任务
        addHostTask.retryAddHost(taskId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delHost(Long hostId){
        if(hostId == null) {
            logger.warn("del host failed ,hostId is null");
            return;
        }
        //添加任务步骤
        KwTaskInfo t = TaskUtils.buildTask(hostId,0L,"delHost","删除主机",0);
        taskInfoService.insertKwTaskInfo(t);
        //添加子任务
        KwTaskInfo t1 = TaskUtils.buildTask(hostId,t.getId(),"checkHostDelete","删除主机预检查",0);
        taskInfoService.insertKwTaskInfo(t1);
        KwTaskInfo t2 = TaskUtils.buildTask(hostId,t.getId(),"uninstallJwagentService","卸载jwagent代理服务",0);
        taskInfoService.insertKwTaskInfo(t2);
        KwTaskInfo t3 = TaskUtils.buildTask(hostId,t.getId(),"deleteHostInfo","删除主机信息",0);
        taskInfoService.insertKwTaskInfo(t3);

        //开始执行任务
        addHostTask.delHost(t,false);
    }
}
